﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 粒子和UI排序
/// </summary>
[AddComponentMenu("Nirvana/Render/Sorting Order Overrider")]
public class SortingOrderOverrider : MonoBehaviour
{

    [Tooltip("The sorting order you want to override.")]
    [SerializeField]
    private int sortingOrder;

    private Renderer[] _renders;

    public int SortingOrder
    {
        get
        {
            return this.sortingOrder;
        }
        set
        {
            this.sortingOrder = value;
            this.Sort();
        }
    }

    // Start is called before the first frame update
    private void Awake()
    {
        this._renders = base.GetComponentsInChildren<Renderer>(true);
        this.Sort();
    }

    private void OnValidate()
    {
        this.Sort();
    }

    private void Sort()
    {
        if (this._renders == null)
        {
            return;
        }

        foreach (Renderer renderer in this._renders)
        {
            renderer.sortingOrder = this.sortingOrder;
        }
    }
}
